<?php
            
use app\model\Record; 
use app\model\Theme; 


/**
 * Author: cbw
 * Desc: 代言代码
 * Time: 2022/1/14 9:41 下午
 * Email: 17854288794@163.com
 */
class Index_Page extends PluginMobilePage {

    /**
     * Desc:基础参数
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/19 10:50 上午
     */
    function main()
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        if ($fromtype=='wxapp'){
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $fromuid = isset($_P['fromuid'])?$_P['fromuid']:'';
            $workerid = isset($_P['workerid'])?$_P['workerid']:'';
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);
            $page_index = max(1,intval($_P['page']));
            $page_size = 5;
            $page_start = ($page_index-1)*$page_size;
            if (!$themeid || !$theme['id']) {
                return_json(1,'活动入口不正确');
            } else {
                $can_rank_next_page=false;
                $can_help_next_page=false;
                //基本检测
                $fans = $this->checkuser();
                //获取代言排行榜
                $rank_list = DaiYanModel::getUserDaiYanRankList($themeid,$page_start,$page_size,$page_index);
                $rank_total = DaiYanModel::getUserDaiYanRankTotal($themeid);
                $rank_page = ceil($rank_total/$page_size);
                if ($rank_page>$page_index){
                    $can_rank_next_page = true;
                }
                //获取好友助力的记录
                $help_list = DaiYanModel::getFriendHelpFlagByWhere($themeid,$fans['uid'],$page_start,$page_size);
                $help_total = DaiYanModel::getFriendHelpFlagTotal($themeid,$fans['uid']);
                $help_page = ceil($help_total/$page_size);
                if ($help_page>$page_index){
                    $can_help_next_page = true;
                }
                //获取包含自己以及其他的审核通过的代言
                $daiyan_list = DaiYanModel::formatFrontDaiYan($themeid,$fans['uid'],$page_start,$page_size);
                //获取代言模版
                $daiyan_temp_list = $_C['yx_dy_rules']?$_C['yx_dy_rules']:[];

                $format_res = [
                    'rank_list'=>$rank_list,
                    'can_rank_next_page'=>$can_rank_next_page,
                    'help_list'=>$help_list,
                    'can_help_next_page'=>$can_help_next_page,
                    'daiyan_list'=>$daiyan_list,
                    'temp_daiyan_list'=>$daiyan_temp_list,
                ];
                return_json(0,'',$format_res);
            }
        }else{
            return_json(1,'非法请求');
        }
    }

    /**
     * Desc:点赞评论
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/19 10:50 上午
     */
    function liked()
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        if ($fromtype=='wxapp'){
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $fromuid = isset($_P['fromuid'])?$_P['fromuid']:'';
            $workerid = isset($_P['workerid'])?$_P['workerid']:'';
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);
            $help_uid = $_P['help_uid'];
            $target_id = $_P['id'];
            if (!$help_uid || !$target_id){
                return_json(1,'点赞失败');
            }
            if (!$themeid || !$theme['id']) {
                return_json(1,'活动入口不正确');
            } else {
                //基本检测
                $fans = $this->checkuser();
                if ($help_uid==$fans['uid']){
                    return_json(1,'不可给自己点赞');
                }
                $liked_res = DaiYanModel::userLiked($themeid,$fans['uid'],$help_uid,$target_id);
                if ($liked_res){
                    return_json(0,'点赞成功',[]);
                }else{
                    return_json(1,'已经点过赞',[]);
                }
            }
        }else{
            return_json(1,'非法请求');
        }
    }

    /**
     * Desc:提交评论
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/19 10:51 上午
     */
    function commitComment()
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        if ($fromtype=='wxapp'){
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $fromuid = isset($_P['fromuid'])?$_P['fromuid']:'';
            $workerid = isset($_P['workerid'])?$_P['workerid']:'';
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);
            $id = $_P['id'];
            $content = addslashes(htmlspecialchars(trim($_P['content'])));
            if (!$themeid || !$theme['id']) {
                return_json(1,'活动入口不正确');
            } else {
                //基本检测
                $fans = $this->checkuser();
                $return_order = DaiYanModel::getUserRecordItem(['id'=>$id,'themeid'=>$themeid,'status'=>1]);
                if (!$return_order){
                    return_json(1,'评论失败');
                    die();
                }
                if (!$content){
                    return_json(1,'评论内容为空');
                    die();
                }
                $insert_res = DaiYanModel::addUserReturn($themeid,$fans['uid'],$return_order['uid'],$content);
                if ($insert_res){
                    return_json(0,'评论成功');
                }else{
                    return_json(1,'请稍后评论');
                }
                $format_res = [

                ];
                return_json(0,'',$format_res);
            }
        }else{
            return_json(1,'非法请求');
        }
    }

    /**
     * Desc:获取底部按钮
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/21 8:39 下午
     */
    function getMenu()
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        if ($fromtype=='wxapp'){
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $fromuid = isset($_P['fromuid'])?$_P['fromuid']:'';
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);

            if (!$themeid || !$theme['id']) {
                return_json(1,'活动入口不正确');
            } else {
                //基本检测
                $bottombut = [];
                $fans = $this->checkuser();
                $themyrecord = DaiYanModel::getDaiYanItemByWhere(['themeid'=>$themeid,'uid'=>$fans['uid']]);
                $friend_sort = ['error'=>'','sort'=>0];
                $user_sort = ['error'=>'','sort'=>0];
                if (!$themyrecord){
                    //判断是否存在fromuid
                    if (!$fromuid){
                        //我要代言
                        array_push($bottombut,['txt'=>'我要代言','url'=>0]);
                    }else{
                        //判断好友是否存在代言
                        $friend_record = Record::getLastGroupByUid($fromuid,$themeid);
                        if (!$friend_record){
                            array_push($bottombut,['txt'=>'我要代言','url'=>0]);
                        }else{
                            $friend_sort = DaiYanModel::getUserDaiYanSort($themeid,$friend_record['uid']);
                            if (!$friend_sort['error']){
                                array_push($bottombut,['txt'=>'好友代言排名'.$friend_sort['sort'].'名','url'=>2]);
                            }else{
                                array_push($bottombut,['txt'=>'好友'.$friend_sort['error'],'url'=>2]);
                            }
                            array_push($bottombut,['txt'=>'我也要代言','url'=>1]);
                        }
                    }
                }else{
                    //判断存在订单
                    if ($fromuid){
                        //判断好友是否存在代言
                        $friend_record = Record::getLastGroupByUid($fromuid,$themeid);
                        if (!$friend_record){
                            array_push($bottombut,['txt'=>'我的代言','url'=>3]);
                        }else{
                            $friend_sort = DaiYanModel::getUserDaiYanSort($themeid,$friend_record['uid']);
                            if (!$friend_sort['error']){
                                array_push($bottombut,['txt'=>'好友代言排名'.$friend_sort['sort'].'名','url'=>2]);
                            }else{
                                array_push($bottombut,['txt'=>'好友'.$friend_sort['error'],'url'=>2]);
                            }
                            array_push($bottombut,['txt'=>'我的代言','url'=>3]);
                        }
                    }else{
                        $user_sort = DaiYanModel::getUserDaiYanSort($themeid,$fans['uid']);
                        if (!$user_sort['error']){
                            array_push($bottombut,['txt'=>'代言排名'.$user_sort['sort'].'名','url'=>4]);
                        }else{
                            array_push($bottombut,['txt'=>$user_sort['error'],'url'=>4]);
                        }
                        array_push($bottombut,['txt'=>'邀请好友代言','url'=>5]);
                    }
                }
                $format_res = [
                    'friend_sort'=>$friend_sort,
                    'user_sort'=>$user_sort,
                    'bottombut'=>$bottombut,
                ];
                return_json(0,'',$format_res);
            }
        }else{
            return_json(1,'非法请求');
        }
    }

}